﻿Imports System.Drawing
Imports System.IO

Public Class AWSMap
    Inherits Map

    ''' <summary>
    ''' 作者
    ''' </summary>
    Property Author As String

    ''' <summary>
    ''' 地图描述
    ''' </summary>
    Property Description As String

    Public ImgByte(,) As Integer
    Public UnitByte(,) As Integer


    Public u1(,) As Byte
    Public u2(,) As Byte

    Public t1(,) As Byte
    Public t2(,) As Byte

    Sub New()
        Me.New(30, 20)
    End Sub

    Sub New(ByVal Width As Integer, ByVal Height As Integer)
        Name = "unnamed"
        SetSize(Width, Height)
    End Sub

    Public Shadows Sub SetSize(ByVal Width As Integer, ByVal Height As Integer)
        Me.Width = Width
        Me.Height = Height
        ReDim ImgByte(Width - 1, Height - 1)
        ReDim UnitByte(Width - 1, Height - 1)

        ReDim u1(Width - 1, Height - 1)
        ReDim u2(Width - 1, Height - 1)
        ReDim t1(Width - 1, Height - 1)
        ReDim t2(Width - 1, Height - 1)
    End Sub

    Public Function GetMapImage() As Bitmap
        Dim bitmap As New Bitmap(Me.Width * 16, Me.Height * 16)
        Dim g = Graphics.FromImage(bitmap)
        g.Clear(Color.Transparent)
        For i = 0 To Width - 1
            For j = 0 To Height - 1
                g.DrawString(ImgByte(i, j), New Font("宋体", 11), Brushes.Black, i * 16, j * 16)
            Next
        Next
        Return bitmap
    End Function

    Public Shared Function GetTable(ByVal bytes(,) As Integer) As DataTable
        Dim dt As New DataTable
        For i = 0 To bytes.GetUpperBound(0)
            dt.Columns.Add("F" & i, GetType(Integer))
        Next
        For j = 0 To bytes.GetUpperBound(1)
            Dim row = dt.NewRow
            For i = 0 To bytes.GetUpperBound(0)
                row(i) = bytes(i, j)
            Next
            dt.Rows.Add(row)
        Next
        Return dt
    End Function

    Public Shared Function GetTable(ByVal bytes(,) As Byte) As DataTable
        Dim dt As New DataTable
        For i = 0 To bytes.GetUpperBound(0)
            dt.Columns.Add("F" & i, GetType(Byte))
        Next
        For j = 0 To bytes.GetUpperBound(1)
            Dim row = dt.NewRow
            For i = 0 To bytes.GetUpperBound(0)
                row(i) = bytes(i, j)
            Next
            dt.Rows.Add(row)
        Next
        Return dt
    End Function

    Public Shared Function FromFile(ByVal filename As String) As Map
        Dim Map As Map, Parser As ISerializer(Of AWSMap) = New AWSMapSerializer
        Using fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
            Map = Parser.Desrialize(fs)
            fs.Close()
        End Using
        Return Map
    End Function

End Class
